package net.zhengxinyun.performance.App.appCtrl;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.App.entity.EvaluateIndexEnrity;
import net.zhengxinyun.performance.App.entity.EvaluateScoreEntity;
import net.zhengxinyun.performance.App.service.EvalScoreMag.EvalScoreMagService;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.SysUnionCode;
import net.zhengxinyun.performance.filter.EvaluateIndexFilter;
import net.zhengxinyun.performance.filter.EvaluateScoreFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 绩效考评定制
 */
@RestController
@RequestMapping("/app/evamanage")
public class EvaMagCtrl {

    @Autowired
    private EvalScoreMagService evamagService;


    /**
     * 考评项目列表查询
     *
     * @param entity
     * @return
     */
    @PostMapping(value = "/evalists")
    public Result<HashMap<String, Object>> indexlist(@RequestBody EvaluateIndexEnrity entity) {
        HashMap<String, Object> map = new HashMap<>();

        List<EvaluateIndexFilter> evaluateIndexEnrities = evamagService.queryEvaMag(new EvaluateIndexFilter());

        map.put("data", evaluateIndexEnrities);
        return ResultUtils.getResults(Result.Status.SUCCESS, "绩效考评列表", map);
    }

    @PostMapping(value = "/manage")
    public Result<String> insertZhongdian(@RequestBody EvaluateIndexEnrity entity) {

        String type = entity.getType();

        if (StringUtils.isBlank(type)) {

            return ResultUtils.getResults(Result.Status.ERROR, "参数错误", null);

        }
        int i = 0;
        switch (type) {

            case "add":
                entity.setId(null);
                i = evamagService.addEvaMag(entity);

                break;
            case "update":
                if (ObjectUtils.isBlank(entity.getId())) {
                    return ResultUtils.getResults(Result.Status.ERROR, "id参数错误", null);
                }
                i = evamagService.updEvaMag(entity);

                break;
            case "del":
                if (ObjectUtils.isBlank(entity.getId())) {
                    return ResultUtils.getResults(Result.Status.ERROR, "参数错误", null);
                }

                i = evamagService.delEvaMag(entity.getId());

                break;


        }

        if (i == 0) {
            return ResultUtils.getResults(Result.Status.ERROR, "无数据操作、更新", null);
        }


        return ResultUtils.getSuccessResults("操作成功");
    }


    //
    @PostMapping(value = "/scorelist")
    public Result<HashMap<String, Object>> scorelist(@RequestBody EvaluateScoreFilter entity) {
        HashMap<String, Object> map = new HashMap<>();

        Long evaId = entity.getEvaId();

        if (ObjectUtils.isBlank(evaId)) {

            return ResultUtils.getResults(Result.Status.ERROR, "参数错误", null);

        }
        EvaluateScoreFilter filter = new EvaluateScoreFilter();
        filter.setEvaId(evaId);
        filter.setPageSize(20);
        filter.setPageNumber(entity.getPageNumber());
        if(StringUtils.isNotBlank(entity.getLeadName()))
        {
            filter.setLeadName(entity.getLeadName());
        }

        List<EvaluateScoreFilter> evaluateScoreFilters = evamagService.queryEvaScore(filter);
        //List<EvaluateScoreEntity> evaluateScoreEntities = evamagService.queryEvainfo(entity.getEvaId(), null);
        map.put("data", evaluateScoreFilters);
        return ResultUtils.getResults(Result.Status.SUCCESS, "得分列表详情", map);
    }

    /**
     * 考核具体事项
     * @param entity
     * @return
     */
    @PostMapping(value = "/infoManage")
    public Result<String> infoManage(@RequestBody EvaluateScoreEntity entity) {
        String type = entity.getType();
        if (StringUtils.isBlank(type) || ObjectUtils.isBlank(entity.getEvaId()) || StringUtils.isBlank(entity.getLeadName())) {

            return ResultUtils.getResults(Result.Status.ERROR, "参数错误 evaId 和 type 不能为空", null);

        }
        int i = 0;
        switch (type) {

            case "add":
                entity.setId(null);
                i = evamagService.addEvaScore(entity);

                break;
            case "update":
                if (ObjectUtils.isBlank(entity.getId())) {
                    return ResultUtils.getResults(Result.Status.ERROR, "参数错误", null);
                }
               // i = evamagService.updEvaScore(entity);

                EvaluateScoreEntity upd = new EvaluateScoreEntity();
                upd.setId(entity.getId());
                upd.setGrades(entity.getGrades());
                upd.setCalculates(entity.getCalculates());
                i = evamagService.updateScoreByPrimaryKeySelective(upd);
                break;
            case "del":
                if (ObjectUtils.isBlank(entity.getId())) {
                    return ResultUtils.getResults(Result.Status.ERROR, "参数错误", null);
                }

                i = evamagService.delEvaScore(entity.getId());

                break;


        }

        if (i == 0) {
            return ResultUtils.getResults(Result.Status.ERROR, "无数据操作、更新", null);
        }
        return ResultUtils.getSuccessResults("操作成功");
    }

    /**
     * 系统自动生成；
     */
    @PostMapping("/createDataAuto")
    public Result createDataAuto(@RequestBody EvaluateScoreFilter filter) {
        String leadName = filter.getLeadName();
        String connects = filter.getConnects();
        Long evaId = filter.getEvaId();
        String criteriaType = filter.getCriteriaType();
        if (StringUtils.isBlank(leadName)||StringUtils.isBlank(connects)||ObjectUtils.isBlank(evaId)
        || StringUtils.isBlank(criteriaType)
        ) {
            return ResultUtils.getResults("500", "领导id不能为空，leadName,connects,evaId,criteriaType");
        }
        if(StringUtils.isBlank(connects)||"0".equals(connects)){
            return ResultUtils.getResults("500", "无关联系统");
        }
        try {
            List<EvaluateScoreEntity> dataUnionSys = evamagService.getDataUnionSys(connects, Long.parseLong(leadName), criteriaType);
            EvaluateIndexFilter filter1 = new EvaluateIndexFilter();
            filter1.setId(filter.getEvaId());
            List<EvaluateIndexFilter> criteriaEntitys = evamagService.queryEvaMag(filter1);
            EvaluateIndexFilter kaohexiangmu = criteriaEntitys.get(0);
            if(ObjectUtils.isBlank(dataUnionSys)){
                return ResultUtils.getResults("500","无获取数据");
            }
            dataUnionSys.forEach(entity -> entity.setEvaId(evaId));
            EvaluateScoreEntity union = dataUnionSys.get(0);
            int i= 0;
            switch (criteriaType){
                case "1"://累计类， 按照数量
                    int nums = dataUnionSys.size();
                    EvaluateScoreEntity enrity = new EvaluateScoreEntity();
                    enrity.setEvaId(evaId);
                    enrity.setCalculates(nums);
                    enrity.setLeadName(leadName);
                    enrity.setAssessment(kaohexiangmu.getIndexName()+"(督查)");
                    enrity.setSysCode(union.getSysCode());
                   i = evamagService.saveOrUpdateEvaScore(enrity);//保存或者更新
                    break;
                case "2"://非累计诶
                    i = evamagService.batchSaveEvaScore(dataUnionSys);
                    break;
            }
            return ResultUtils.getSuccessResults("已更新"+i+"条数据");
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     *批量修改档次
     */
    @PostMapping("/batchUpdEvaInfos")
    public Result batchUpdEvaInfos(@RequestBody EvaluateScoreFilter filter) {

       /* String leadName = filter.getLeadName();
        String connects = filter.getConnects();*/
        String optionType = filter.getOptionType();

      //  Long evaId = filter.getEvaId();
        String idlists = filter.getIdlists();
        String grades = filter.getGrades();
        int calculates = filter.getCalculates();

        String criteriaType = filter.getCriteriaType();//类型；
    /*  if(StringUtils.isBlank(calculates)){
            return ResultUtils.getResults("500", "累计类 ，计量值不能为空 ");
        }*/
        if (StringUtils.isBlank(idlists)) {
            return ResultUtils.getResults("500", "id不能为空");
        }
        int i  =0;

        List<String> strings = Util.strToList(idlists);

        switch (optionType){
            case "update":

                for (String item : strings) {
                    EvaluateScoreEntity entity = new EvaluateScoreEntity();
                    entity.setId(Long.parseLong(item));
                    if("1".equals(criteriaType.trim())){
                        entity.setCalculates(calculates);
                    }else {
                        if (StringUtils.isBlank(grades)) {
                            return ResultUtils.getResults("500", "档次不能为空");
                        }
                        entity.setGrades(grades);
                    }
                    int t=evamagService.updateScoreByPrimaryKeySelective(entity);
                    i=i+t;
                }
                break;
            case "del":
                for (String item : strings) {
                    int t=evamagService.delEvaScore(Long.parseLong(item));
                    i=i+t;
                }

        }


            if(i==0){
                return ResultUtils.getResults("500","无数据更新");
            }

        return ResultUtils.getSuccessResults();
    }

    /**
     * 项目详情
     */

}
